package one.bugu.android.demon.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import one.bugu.android.demon.R;
import one.bugu.android.demon.util.OnViewClickListener;

public class DialogContent extends Dialog {

    private Context mContext;
    private LinearLayout ll_dialog_content;
    private ImageView iv_dialog_cancle;
    private double heightScale = 0.8;

    public DialogContent(@NonNull Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public DialogContent(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        this.mContext = context;
        initView();
    }

    protected DialogContent(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        this.mContext = context;
        initView();
    }

    /**
     * @param context
     * @param themeResId
     * @param heightScale 高度显示比例
     */
    public DialogContent(@NonNull Context context, int themeResId, double heightScale) {
        super(context, themeResId);
        this.mContext = context;
        this.heightScale = heightScale;
        initView();
    }

    private void initView() {
        View view = View.inflate(mContext, R.layout.dialog_content, null);
        ll_dialog_content = view.findViewById(R.id.ll_dialog_content);
        iv_dialog_cancle = view.findViewById(R.id.iv_dialog_cancle);
        setContentView(view);

        //设置窗体
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        WindowManager wm = this.getWindow().getWindowManager();
        int width = wm.getDefaultDisplay().getWidth();
        int height = wm.getDefaultDisplay().getHeight();
        lp.width = (int) (0.8 * width);
        //设置弹窗高度
        if (heightScale != 0) {
            lp.height = (int) (heightScale * height);
        }
        dialogWindow.setAttributes(lp);

        iv_dialog_cancle.setOnClickListener(new OnViewClickListener() {
            @Override
            protected void singleClick(View view) {
                if (DialogContent.this.isShowing()) {
                    DialogContent.this.dismiss();
                }
            }
        });
    }

    /**
     * 设置显示view
     *
     * @param view
     */
    public void setView(@NonNull View view) {
        setViewAndBackground(view, -1);
    }

    /**
     * 设置显示view及bg
     *
     * @param view
     */
    public void setViewAndBackground(@NonNull View view, int bgRes) {
        if (bgRes != -1) {
            ll_dialog_content.setBackgroundResource(bgRes);
        }
        if (null != ll_dialog_content && null != view) {
            ll_dialog_content.removeAllViews();
            ll_dialog_content.addView(view);
        }
    }
}
